//package com.bestcem.xm.om.util;
//
//import com.alibaba.fastjson.JSON;
//import lombok.AccessLevel;
//import lombok.NoArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//
//import javax.servlet.http.HttpServletRequest;
//import java.net.InetAddress;
//import java.util.Enumeration;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * ip工具类
// *
// * @author jie.yao <jie.yao@idiaoyan.com>
// * @version v1.0
// * @date 2021/8/19 14:03
// */
//@Slf4j
//@NoArgsConstructor(access = AccessLevel.PRIVATE)
//public class IpAddressUtil {
//
//    public static String getIpAddress(HttpServletRequest request) {
//        if (log.isInfoEnabled()) {
//            log.info("remoteAddr:{}", request.getRemoteAddr());
//            log.info("remoteHost:{}", request.getRemoteHost());
//            Enumeration<String> headerNames = request.getHeaderNames();
//            Map<String, Object> headerMap = new HashMap<>();
//            while (headerNames.hasMoreElements()) {
//                String key = headerNames.nextElement();
//                headerMap.put(key, request.getHeader(key));
//            }
//            log.info("request heeaders:{}", JSON.toJSONString(headerMap));
//        }
//        String ipAddress = null;
//        try {
//            String unknown = "unknown";
//            ipAddress = request.getHeader("x-forwarded-for");
//            if (ipAddress != null && ipAddress.length() > 0 && ipAddress.contains(",")) {
//                ipAddress = ipAddress.split(",")[0].trim();
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("x-real-ip");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getRemoteAddr();
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("x-forwarded-for");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getHeader("WL-Proxy-Client-IP");
//            }
//            if (ipAddress == null || ipAddress.length() == 0 || unknown.equalsIgnoreCase(ipAddress)) {
//                ipAddress = request.getRemoteAddr();
//                if ("127.0.0.1".equals(ipAddress)) {
//                    // 根据网卡取本机配置的IP
//                    InetAddress inet = InetAddress.getLocalHost();
//                    ipAddress = inet.getHostAddress();
//                }
//            }
//            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割, "***.***.***.***".length() = 15
//            if (ipAddress != null && ipAddress.length() > 15 && ipAddress.contains(",")) {
//                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
//            }
//        } catch (Exception e) {
//            ipAddress = "";
//        }
//        return ipAddress;
//    }
//}
